import pygame, random
from math import *
armang_init = 15
armang_final = 180 - armang_init
bodyang_init = 90
goalx = 100
goaly = 30
gk_size = 100

class Goalkeeper:
	
	def __init__(self, screen, distance):
		self.__distance = distance
		self.__armang = armang_init # right arm angle with respect to body
		self.__bodyang = 0 # with respect to floor
		self.__bodyang_final = 0
		self.__dx = 0
		self.__dy = 0
		self.__startx = screen.get_width()/2 - 45
		self.__starty = screen.get_height()/2 - 60
		self.__originalbody = self.__generate_body()
		self.__originalleft, self.__originalright = self.__generate_arms()
		self.draw(screen)
	
	def draw(self, screen):
		self.__startx += self.__dx
		self.__starty += self.__dy
		self.__draw_body(screen)
		self.__draw_arm(screen)
		self.__bodyang += (self.__bodyang_final - bodyang_init)/self.__distance
				
	def __draw_body(self, screen):
		(img,pos)=self.__rotate_itself(self.__originalbody,self.__bodyang)
		
		screen.blit(img,(pos[0]+self.__startx,pos[1]+self.__starty))
		
	def __draw_arm(self, screen):
		#self.__armang += (armang_final - armang_init)/self.__distance
		(left_arm,pleft) = self.__rotate_itself(self.__originalleft, self.__armang + self.__bodyang)
		(right_arm,pright) = self.__rotate_itself(self.__originalright, -self.__armang + self.__bodyang)
		screen.blit(left_arm, (pleft[0]+self.__startx +3,pleft[1]+self.__starty+3))
		screen.blit(right_arm, (pright[0]+self.__startx -4,pright[1]+self.__starty+3))
		


	def set_destiny(self, destiny):
		defendx = random.randint(-goalx, goalx)
		defendy = random.randint(-goaly, goaly/5)
			
		if (defendx - destiny[0]+self.__startx)**2 + ( defendy - destiny[1]+self.__starty)**2 < 10000:
			print "defendeu"
			defendx, defendy = destiny[0] - self.__startx, destiny[1] - self.__starty
		else:
			print "gooool"
			
		area = self.__find_area((defendx, defendy))
		self.__bodyang_final = self.__final_ang(area)

		self.__dx = defendx/self.__distance
		self.__dy = defendy/self.__distance

	
	def __generate_body(self):
		body = pygame.image.load('goalkeeper_noarms.png')
		body = pygame.transform.smoothscale(body, (gk_size, gk_size))
		return body
	
	def __generate_arms(self):
		left_arm = pygame.image.load('left_arm.png')
		left_arm = pygame.transform.smoothscale(left_arm, (gk_size, gk_size))
		right_arm = pygame.image.load('right_arm.png')
		right_arm = pygame.transform.smoothscale(right_arm, (gk_size, gk_size))
		return left_arm, right_arm
	
	def __find_area(self, destiny):
		area = 0
		if destiny[1] < -3*goaly/5:
			area += 4	# lower half of goal
		if destiny[0] < -goalx/2:
			pass
		elif destiny[0] < 0:
			area += 1
		elif destiny[0] < goalx/2:
			area += 2
		else:
			area += 3
		
		return area
	
	def __final_ang(self, area):
		# Holds the angles according the given area		
		ang = [135, 110, 70, 45, 180, 90, 90, 0]
		return ang[area]
		
	def __rotate_itself(self,image,angle):
		rot_img = pygame.transform.rotate(image,angle)
		img_rect = rot_img.get_rect()
		img_rect.center = image.get_rect().center
		return (rot_img, img_rect)
	
	def __rotate(self, image, angle): # corrigir
		return pygame.transform.rotate(image, angle)
		
	
